<%--
  Created by IntelliJ IDEA.
  User: yujiajing
  Date: 2022/10/10
  Time: 10:08
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <%--  引入jQuery包  --%>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
  <!-- 引入layui css -->
  <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" />

  <!-- 引入layui js -->
  <script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
  <meta charset="utf-8">

  <style type="text/css">
    .container{
      width: 420px;
      height: 320px;
      min-height: 320px;
      max-height: 320px;
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      margin: auto;
      padding: 20px;
      z-index: 130;
      border-radius: 8px;
      background-color: #fff;
      box-shadow: 0 3px 18px rgba(100, 0, 0, .5);
      font-size: 16px;
    }
    .close{
      background-color: white;
      border: none;
      font-size: 18px;
      margin-left: 410px;
      margin-top: -10px;
    }

    .layui-input{
      border-radius: 5px;
      width: 300px;
      height: 40px;
      font-size: 15px;
    }
    .layui-form-item{
      margin-left: -20px;
    }
    #logoid{
      margin-top: -16px;
      padding-left:150px;
      padding-bottom: 15px;
    }
    .layui-btn{
      margin-left: -50px;
      border-radius: 5px;
      width: 350px;
      height: 40px;
      font-size: 15px;
    }
    .verity{
      width: 120px;
    }
    .font-set{
      font-size: 13px;
      text-decoration: none;
      margin-left: 120px;
    }
    a:hover{
      text-decoration: underline;
    }
   #canvas{
       float: right;
       display: inline-block;
       border: 1px solid #ccc;
       border-radius: 5px;
       cursor: pointer;
   }
  </style>
</head>
<body>
<form id="loginform" class="layui-form" action="CartView.jsp" method="post" onsubmit=false>
  <div class="container">
    <button class="close" title="关闭">X</button>
    <div class="layui-form-mid layui-word-aux">
<%--      <img id="logoid" src="06.png" height="35">--%>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">用户名</label>
      <div class="layui-input-block">
        <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">密 &nbsp;&nbsp;码</label>
      <div class="layui-input-inline">
        <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">

      </div>
      <!-- <div class="layui-form-mid layui-word-aux">辅助文字</div> -->
    </div>

    <div class="layui-form-item" style="width: auto">
      <label class="layui-form-label">验证码</label>
      <div class="layui-input-inline">
        <input type="text" id="VerificationCode" required  lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input verity">
      </div>
            <canvas id="canvas" width="100" height="39"></canvas>

      <!-- <div class="layui-form-mid layui-word-aux">辅助文字</div> -->

    </div>
    <!-- 			  <div class="layui-form-item">
                        <label class="layui-form-label">记住密码</label>
                        <div class="layui-input-block">
                          <input type="checkbox" name="close" lay-skin="switch" lay-text="ON|OFF">
                        </div>
                  </div> -->

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button id="loginButton" class="layui-btn" lay-submit lay-filter="formDemo">登陆</button>
      </div>
    </div>
    <a href="" class="font-set">忘记密码?</a>  <a href="InsertUser.jsp" class="font-set">立即注册</a>
  </div>
</form>

<script>
  layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

    //日期
     laydate.render({
       elem: '#date'
     });
     laydate.render({
       elem: '#date1'
     });

     //创建一个编辑器
     var editIndex = layedit.build('LAY_demo_editor');

     //自定义验证规则
     form.verify({
       title: function(value){
         if(value.length < 5){
           return '标题至少得5个字符啊';
         }
       }
       ,pass: [
         /^[\S]{6,12}$/
         ,'密码必须6到12位，且不能出现空格'
       ]
       ,content: function(value){
         layedit.sync(editIndex);
       }
     });

     //监听指定开关
     form.on('switch(switchTest)', function(data){
       layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
         offset: '6px'
       });
       layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
     });

    //监听提交
    form.on('submit(demo1)', function(data){
      layer.alert(JSON.stringify(data.field), {
        title: '最终的提交信息'
      })
      return false;
    });

  });
</script>

<%--利用canvas生成验证码--%>
<script>
    var show_num=[];
    var String_num=null;
    $(function()
    {
        draw(show_num); //第一次先生成show_num
        $("#canvas").on('click',function()
        {
            draw(show_num);//带入show_num

            String_num=show_num.join("");
            //alert(String_num);

            //自动填写，以便测试
       /*     document.getElementById("VerificationCode").value=String_num;*/
        })
    });
    function draw(show_num) {
        var canvas_width = $('#canvas').width();
        var canvas_height = $('#canvas').height();
        var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
        var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
        canvas.width = canvas_width;
        canvas.height = canvas_height;
        var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
        var aCode = sCode.split(",");
        var aLength = aCode.length;//获取到数组的长度

        for (var i = 0; i <= 3; i++) {
            var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
            var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
            var txt = aCode[j];//得到随机的一个内容
            show_num[i] = txt.toLowerCase();//加入到show_num数组中
            var x = 10 + i * 20;//文字在canvas上的x坐标
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
            context.font = "bold 23px 微软雅黑";

            context.translate(x, y);
            context.rotate(deg);

            context.fillStyle = randomColor();
            context.fillText(txt, 0, 0);

            context.rotate(-deg);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //验证码上显示线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 30; i++) { //验证码上显示小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }



    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    $(function()
    {

        $("#loginButton").on('click',function()
        {
            //登录按钮被按下后，比较画布输出的验证码和用户输入的验证码
            if(document.getElementById("VerificationCode").value=String_num){
                // alert("验证码校验成功！");
                document.getElementById("loginform").onsubmit=true;//允许表单提交数据
            }else if (document.getElementById("VerificationCode").value !=null)
            { alert("验证码校验失败");//不相等且不为空
            return };
                })
    });
</script>

</body>
</html>

